package com.jdym.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdym.R;
import com.jdym.entity.Business;
import com.jdym.entity.CoBank;
import com.jdym.entity.Loan;
import com.jdym.entity.Uploadfile;
import com.jdym.entity.vo.loanInfo;
import com.jdym.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 申请贷款表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-09-20
 */
@Controller
@RequestMapping("/loan")
public class LoanController {
@Autowired
    LoanService loanService;
@Autowired
    CoBankService coBankService;
@Autowired
    UploadfileService uploadfileService;
@Autowired
    FirmService firmService;
@Autowired
    BusinessService businessService;
    @RequestMapping("/getAllCoBand")
    public String getAllCoBand(Model model){
//        获取到所有的合作银行
      List<CoBank> coBanks= coBankService.getBaseMapper().selectList(null);

        model.addAttribute("coBanks",coBanks);
        return "borrow_details";
    }
    @ResponseBody
    @RequestMapping("/findRateById")
    public CoBank findRateById(int bankId){

     return coBankService.getBaseMapper().selectById(bankId);
    }
//提交贷款申请
    @RequestMapping("/applyloan")
    public String applyLoan(Loan loan){
        System.out.println(loan);
        loanService.getBaseMapper().insert(loan);
        int loanId=loan.getLoanId();

        System.out.println("贷款id"+loan.getFirmId());
       return "redirect:/loan/borrowSuc/"+loan.getFirmId()+"/"+loanId/*+loan.getFirmId()*/;
    }

    @RequestMapping(value = "/borrowSuc/{firmId}/{loanId}")
    public String borrowSuc(@PathVariable int firmId,@PathVariable int loanId,Model model){

        Uploadfile uploadfile= uploadfileService.getBaseMapper().selectById(firmId);
        model.addAttribute("uploadfile",uploadfile);
        model.addAttribute("loanId",loanId);
        return "borrow_success";
    }
    @ResponseBody
    @RequestMapping(value = "/getLoanByFirmId/{firmId}")
    public R getLoanByFirmId(@PathVariable int firmId){

    List<loanInfo> loanInfos=  loanService.getLoanInfo(firmId);

      return R.ok().data("loanInfo",loanInfos);
    }
    //        查找还没审批的贷款

    @ResponseBody
    @RequestMapping(value = "/selectLoanStatusis2/{firmId}")
    public R selectLoanStatusis2(@PathVariable int firmId){

        List<loanInfo> loanInfos=  loanService.selectLoanStatusis2(firmId);

        return R.ok().data("loanInfo",loanInfos);

    }

    @ResponseBody
    @RequestMapping(value = "/deleteByLoanStatus/{loanId}")
    public R deleteByLoanStatus(@PathVariable int loanId){
        loanService.getBaseMapper().deleteById(loanId);
        return R.ok();
    }
}

